package com.springboot2.service.Impl;

import com.springboot2.entity.Goods;
import com.springboot2.entity.GoodsCategory;
import com.springboot2.entity.Org;
import com.springboot2.mapper.GoodsCategoryMapper;
import com.springboot2.mapper.GoodsMapper;
import com.springboot2.service.GoodsCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public void add(GoodsCategory goodsCategory) {

        Date date = new Date();
        goodsCategory.setCreateTime(date);
        goodsCategory.setIsDel(1);
        goodsCategoryMapper.add(goodsCategory);
    }

    @Override
    public void del(int id) {
        goodsCategoryMapper.del(id);
    }

    @Override
    public void update(GoodsCategory goodsCategory) {
        Date date = new Date();
        goodsCategory.setCreateTime(date);
        goodsCategory.setIsDel(1);
        goodsCategoryMapper.update(goodsCategory);
    }

    @Override
    public GoodsCategory findById(int id) {
        return goodsCategoryMapper.findById(id);
    }

    @Override
    public List<GoodsCategory> findByPage(int pageNo, int pageSize) {



        Map<String,Integer> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        return goodsCategoryMapper.findByPage(map);
    }

    @Override
    public int count() {
        return goodsCategoryMapper.count();
    }

    @Override
    public List<GoodsCategory> findByPageParams(int pageNo, int pageSize, String title) {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("title",title+"%");
        return goodsCategoryMapper.findByPageParams(map);
    }

    @Override
    public int countParams(String title) {
        return goodsCategoryMapper.countParams(title);
    }

    @Override
    public List<GoodsCategory> findAll() {

        List<Goods> goods = goodsMapper.findAll();
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.findAll();


            for(GoodsCategory goodsCategory: goodsCategories){
                List<Goods> list = goodsMapper.findByCategory(goodsCategory.getTitle());
                goodsCategory.setChildren(list);
            }


        return goodsCategories;
    }
}
